package com.itheima.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class MybatisUtil {
    public static SqlSession getSqlSession() throws IOException {
        //加载配置文件
        InputStream ins = Resources.getResourceAsStream("mybatis-config.xml");
        //建立sqlSessionFactory构造器
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        //建立SqlSession工厂
        SqlSessionFactory build = sqlSessionFactoryBuilder.build(ins);
        //建立sqlSession
        SqlSession sqlSession = build.openSession(true);
        return sqlSession;
    }
}
